<?php
use common\Tools;
use common\components\wtools\tools\FileHelper;

if (!function_exists('dd')) {
    function dd(...$args)
    {
        if (function_exists('dump')) {
            dump(...$args);
        } else {
            var_dump(...$args);
        }
        exit;
    }
}

define("YII_PROJECT_ROOT", dirname(dirname(__DIR__))); // 根目录
/** @var array $bootstrap */
$bootstrap = require_once('bootstrap-local.php');

Yii::setAlias('@common', YII_PROJECT_ROOT.'/common');
Yii::setAlias('@api', YII_PROJECT_ROOT . '/api');
Yii::setAlias('@adminApi', YII_PROJECT_ROOT . '/adminApi'); // 后台管理
Yii::setAlias('@gylApi', YII_PROJECT_ROOT . '/gylApi'); // 供应链
Yii::setAlias('@h5Api', YII_PROJECT_ROOT . '/h5Api');
Yii::setAlias('@dlApi', YII_PROJECT_ROOT . '/dlApi'); // 代理
Yii::setAlias('@xcxApi', YII_PROJECT_ROOT . '/xcxApi'); // 客户小程序
Yii::setAlias('@admin', YII_PROJECT_ROOT. '/admin');
Yii::setAlias('@home', YII_PROJECT_ROOT. '/home');
Yii::setAlias('@console', YII_PROJECT_ROOT. '/console');

$base_url_head = Tools::getCurrentScheme();
if (isset($_SERVER['HTTP_ALI_SWIFT_STAT_HOST'])) { // cdn加速 阿里云
    $THIS_SERVER_BASE_URL = $base_url_head.$_SERVER['HTTP_ALI_SWIFT_STAT_HOST'];
    $_SERVER['HTTP_HOST'] = $_SERVER['SERVER_NAME'] = $_SERVER['HTTP_ALI_SWIFT_STAT_HOST'];
} else {
    if (Tools::isCli()){
        $THIS_SERVER_BASE_URL = $bootstrap['this_server_base_uri'];
    } else {
        $THIS_SERVER_BASE_URL = $base_url_head.$_SERVER['HTTP_HOST'];
    }
}

define("THIS_SERVER_BASE_URI", $THIS_SERVER_BASE_URL); // 域名地址
define("THIS_SERVER_NAME", $bootstrap['this_server_name']); // 应用名
define("THIS_SERVER_IS_CROS", $bootstrap['this_server_is_cros']); // 是否需要cros

Yii::setAlias('@wroot', YII_PROJECT_ROOT . '/web');
Yii::setAlias('@wurl', '/');
Yii::setAlias('@waburl', THIS_SERVER_BASE_URI);
Yii::setAlias('@static_root', YII_PROJECT_ROOT . '/web/static');
Yii::setAlias('@static_url', '/static');
Yii::setAlias('@static_aburl', THIS_SERVER_BASE_URI.'/static');
Yii::setAlias('@storage_root', YII_PROJECT_ROOT . '/web/storage');
Yii::setAlias('@storage_url', '/storage');
Yii::setAlias('@storage_aburl', THIS_SERVER_BASE_URI.'/storage');
Yii::setAlias('@others_url', '/others');
Yii::setAlias('@others_aburl', THIS_SERVER_BASE_URI.'/others');
if(YII_ENV_DEV){
    Yii::setAlias('@uploads_root', YII_PROJECT_ROOT . '/web/storage/uploads/dev');
    Yii::setAlias('@uploads_url', '/storage/uploads/dev');
    Yii::setAlias('@uploads_aburl', THIS_SERVER_BASE_URI.'/storage/uploads/dev');
}else{
    Yii::setAlias('@uploads_root', YII_PROJECT_ROOT . '/web/storage/uploads/prod');
    Yii::setAlias('@uploads_url', '/storage/uploads/prod');
    Yii::setAlias('@uploads_aburl', THIS_SERVER_BASE_URI.'/storage/uploads/prod');
}
if (!is_dir(Yii::getAlias('@uploads_root'))){
    FileHelper::createDirectory(Yii::getAlias('@uploads_root'), 0777);
}

Yii::$classMap['yii\caching\Cache'] = '@common/rewrites/caching/Cache.php';
Yii::$classMap['moonland\phpexcel\Excel'] = '@common/rewrites/moonlandsoft/yii2-phpexcel/Excel.php';

Yii::$container->set(\yii\widgets\LinkPager::class, [
    'maxButtonCount' => 10,
    'firstPageLabel' => '首页',
    'lastPageLabel' => '末页',
    'prevPageLabel'=>'上一页',
    'nextPageLabel'=>'下一页',
]);

Yii::$container->set(\yii\data\Pagination::class, [
    'defaultPageSize' => 10,
]);
